package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        Long userid = UserThreadLocal.get();
        alertData.setUserId(userid);
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 处理报警数据
     * @param dto
     */
    @Transactional
    @Override
    public void handleAlertData(HandleAlertDataDto dto) {
        //获取当前登录用户
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        //将Dto数据复制到AlertData对象
        AlertData alertData = getById(dto.getId());
        //获取iot_id
        List<AlertData> list = list(Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getIotId, alertData.getIotId())
                .eq(AlertData::getFunctionId, alertData.getFunctionId())
                .eq(AlertData::getProductKey, alertData.getProductKey())
                .eq(AlertData::getCreateTime, alertData.getCreateTime()));
        List<AlertData> collect = list.stream().map(item -> {
            //获取处理结果
            item.setProcessingResult(dto.getProcessingResult());
            //设置处理时间
            item.setProcessingTime(dto.getProcessingTime());
            //设置处理人ID
            item.setProcessorId(currentUser.getUserId());
            //设置处理人名称
            item.setProcessorName(currentUser.getNickName());
            //设置处理状态
            item.setStatus(1);
            return item;
        }).collect(Collectors.toList());

        updateBatchById(collect);
    }

}
